<?php
namespace common\modules\user\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use common\modules\user\models\query\UserGroupQuery;

/**
 * This is the model class for table "{{%user_group}}".
 *
 * @property integer $id
 * @property integer $parent_id
 * @property string $type
 * @property string $name
 * @property string $description
 * @property string $group_rank
 * @property integer $created_at
 *
 * @property UserGroup $parent
 * @property UserGroup[] $userGroups
 */
class UserGroup extends \yii\db\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user_group}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'updatedAtAttribute' => false
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                [
                    'type',
                    'name'
                ],
                'required'
            ],
            [
                [
                    'parent_id'
                ],
                'integer'
            ],
            [
                [
                    'description'
                ],
                'string'
            ],
            [
                [
                    'type',
                    'group_rank'
                ],
                'string',
                'max' => 20
            ],
            [
                [
                    'name'
                ],
                'string',
                'max' => 32
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'ID'),
            'parent_id' => Yii::t('common', 'Parent ID'),
            'type' => Yii::t('common', 'Type'),
            'name' => Yii::t('common', 'Name'),
            'description' => Yii::t('common', 'Description'),
            'group_rank' => Yii::t('common', 'Group Rank'),
            'created_at' => Yii::t('common', 'Created At')
        ];
    }

    /**
     *
     * @return \yii\db\ActiveQuery
     */
    public function getParent()
    {
        return $this->hasOne(UserGroup::className(), [
            'id' => 'parent_id'
        ]);
    }

    /**
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUserGroups()
    {
        return $this->hasMany(UserGroup::className(), [
            'parent_id' => 'id'
        ]);
    }

    /**
     * @inheritdoc
     *
     * @return UserGroupQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new UserGroupQuery(get_called_class());
    }
}
